﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.ProductionTasks;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.ProductionTasks
{
    /// <summary>
    /// 根据生产任务ID进行查询列表Handler
    /// </summary>
    public class SearchProductionTasksIDHandler : IRequestHandler<SearchProductionTasksIDCommand, Result<ProductionTasksDto>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 任务类型表仓储
        /// </summary>
        public readonly IBaseRepository<TaskType> taskTyperepository;
        /// <summary>
        /// 生产任务表仓储
        /// </summary>
        public readonly IBaseRepository<Productiontasks> productiontasksrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchProductionTasksIDHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="mapper"></param>
        /// <param name="taskTyperepository"></param>
        /// <param name="productiontasksrepository"></param>
        /// <param name="logger"></param>
        public SearchProductionTasksIDHandler(ISqlSugarClient db, IMapper mapper, IBaseRepository<TaskType> taskTyperepository, IBaseRepository<Productiontasks> productiontasksrepository, ILogger<SearchProductionTasksIDHandler> logger)
        {
            _db = db;
            this.mapper = mapper;
            this.taskTyperepository = taskTyperepository;
            this.productiontasksrepository = productiontasksrepository;
            this.logger = logger;
        }
        /// <summary>
        /// 根据生产任务ID进行查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<ProductionTasksDto>> Handle(SearchProductionTasksIDCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<ProductionTasksDto>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<Productiontasks, TaskType>((a, b) => new JoinQueryInfos(
                    JoinType.Inner, a.TaskTypeId == b.TaskTypeId
                    ))
                    .Select((a, b) => new ProductionTasksDto()
                    {
                        ProductiontasksId = a.ProductiontasksId,
                        ProductiontasksCode = a.ProductiontasksCode,
                        ProductiontasksName = a.ProductiontasksName,
                        ProductionQuantity = a.ProductionQuantity,
                        StartTime = a.StartTime,
                        EndTime = a.EndTime,
                        IsStatus = a.IsStatus,
                        MaterialId = a.MaterialId,
                        TaskTypeId = a.TaskTypeId,
                        TaskTypeName = b.TaskTypeName,
                        IsDel = a.IsDel,
                        AuditStatus = a.AuditStatus,
                        CreatedOwner = a.CreatedOwner,
                        CreatedTime = a.CreatedTime
                    }).Where(a => a.IsDel == 1&&a.ProductiontasksId==request.ProductiontasksId).FirstAsync();
                result.Data = query;
                result.Msg = "根据生产任务ID进行查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据生产任务ID进行查询失败" + ex.Message;
                logger.LogError("根据生产任务ID进行查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
